/**
 * @fileOverview The "stylescombo" plugin.
 */
(function() {
   'use strict';
   VED.plugins.add( 'stylescombo', {
      requires: 'richcombo',
      lang: 'en,ru', // %REMOVE_LINE_CORE%

      init: function( editor ) {
         var config = editor.config,
            lang = editor.lang.stylescombo,
            styles = {},
            stylesList = [],
            combo,
            allowedContent = [];

         editor.on( 'stylesSet', function( evt ) {
            var stylesDefinitions = evt.data.styles;

            if ( !stylesDefinitions )
               return;

            var style, styleName;

            // Put all styles into an Array.
            for ( var i = 0, count = stylesDefinitions.length; i < count; i++ ) {
               var styleDefinition = stylesDefinitions[ i ];

               if ( editor.blockless && ( styleDefinition.element in VED.dtd.$block ) )
                  continue;

               styleName = styleDefinition.name;

               style = new VED.style( styleDefinition );

               if ( !editor.filter.customConfig || editor.filter.check( style ) ) {
                  style._name = styleName;
                  style._.enterMode = config.enterMode;

                  // Weight is used to sort styles (#9029).
                  style._.weight = i + ( style.type == VED.STYLE_OBJECT ? 1 : style.type == VED.STYLE_BLOCK ? 2 : 3 ) * 1000;

                  styles[ styleName ] = style;
                  stylesList.push( style );
                  allowedContent.push( style );
               }
            }

            // Sorts the Array, so the styles get grouped by type in proper order (#9029).
            stylesList.sort( function( styleA, styleB ) { return styleA._.weight - styleB._.weight; } );
         } );

         editor.ui.addRichCombo( 'Styles', {
            label: lang.label,
            title: lang.panelTitle,
            toolbar: 'styles,10',
            allowedContent: allowedContent,

            panel: {
               css: [ VED.skin.getPath( 'editor' ) ].concat( config.contentsCss ),
               multiSelect: true,
               attributes: { 'aria-label': lang.panelTitle }
            },

            init: function() {
               var style, styleName, lastType, type, i, count;

               // Loop over the Array, adding all items to the
               // combo.
               for ( i = 0, count = stylesList.length; i < count; i++ ) {
                  style = stylesList[ i ];
                  styleName = style._name;
                  type = style.type;

                  if ( type != lastType ) {
                     this.startGroup( lang[ 'panelTitle' + String( type ) ] );
                     lastType = type;
                  }

                  this.add( styleName, style.type == VED.STYLE_OBJECT ? styleName : style.buildPreview(), styleName );
               }

               this.commit();
            },

            onClick: function( value ) {
               editor.focus();
               editor.fire( 'saveSnapshot' );

               var style = styles[ value ],
                  elementPath = editor.elementPath();

               editor[ style.checkActive( elementPath ) ? 'removeStyle' : 'applyStyle' ]( style );
               editor.fire( 'saveSnapshot' );
            },

            onRender: function() {
               editor.on( 'selectionChange', function( ev ) {
                  var currentValue = this.getValue(),
                     elementPath = ev.data.path,
                     elements = elementPath.elements;

                  // For each element into the elements path.
                  for ( var i = 0, count = elements.length, element; i < count; i++ ) {
                     element = elements[ i ];

                     // Check if the element is removable by any of
                     // the styles.
                     for ( var value in styles ) {
                        if ( styles[ value ].checkElementRemovable( element, true ) ) {
                           if ( value != currentValue )
                              this.setValue( value );
                           return;
                        }
                     }
                  }

                  // If no styles match, just empty it.
                  this.setValue( '' );
               }, this );
            },

            onOpen: function() {
               var selection = editor.getSelection(),
                  element = selection.getSelectedElement(),
                  elementPath = editor.elementPath( element ),
                  counter = [ 0, 0, 0, 0 ];

               this.showAll();
               this.unmarkAll();
               for ( var name in styles ) {
                  var style = styles[ name ],
                     type = style.type;

                  // Check if block styles are applicable.
                  if ( type == VED.STYLE_BLOCK && !elementPath.isContextFor( style.element ) ) {
                     this.hideItem( name );
                     continue;
                  }

                  if ( style.checkActive( elementPath ) )
                     this.mark( name );
                  else if ( type == VED.STYLE_OBJECT && !style.checkApplicable( elementPath ) ) {
                     this.hideItem( name );
                     counter[ type ]--;
                  }

                  counter[ type ]++;
               }

               if ( !counter[ VED.STYLE_BLOCK ] )
                  this.hideGroup( lang[ 'panelTitle' + String( VED.STYLE_BLOCK ) ] );

               if ( !counter[ VED.STYLE_INLINE ] )
                  this.hideGroup( lang[ 'panelTitle' + String( VED.STYLE_INLINE ) ] );

               if ( !counter[ VED.STYLE_OBJECT ] )
                  this.hideGroup( lang[ 'panelTitle' + String( VED.STYLE_OBJECT ) ] );
            },

            // Force a reload of the data
            reset: function() {
               if ( combo ) {
                  delete combo._.panel;
                  delete combo._.list;
                  combo._.committed = 0;
                  combo._.items = {};
                  combo._.state = VED.TRISTATE_OFF;
               }
               styles = {};
               stylesList = [];
            }
         });
      }
   });
})();





